package com.minisoft.utils.excel;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.fasterxml.jackson.core.JsonProcessingException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author JueSu
 * @description: TODO
 * @date 2023/7/28 13:00
 */
public class ExportPAResult {

    public static void main(String[] args) throws JsonProcessingException {
        String outputFilePath = "D:\\temp/paResult-"+System.currentTimeMillis()+".xlsx";
        // Read JSON data from the input file
        String jsonData = FileUtil.readString(ExportPAResult.class.getResource("/paResult.txt"),"utf-8");

        // Parse JSON data to get rows and column_list
        JSONArray array = JSONUtil.parseObj(jsonData).getJSONObject("data").getJSONArray("clientList");
        List<List<String>> rows = new ArrayList<>();
        List<String> columnList = ListUtil.toList("姓名","手机号","证件号码","电子邮箱","家庭地址");

        String url = "https://pssp.pa18.com/crm/clientManage/queryClientDetail.dhtml?ajaxRequest=SMTResourceNew";
        for (Object o : array) {
            JSONObject obj = (JSONObject) o;
            JSONObject p = new JSONObject();
            p.set("clientMainId",obj.get("clientMainId"));
            Map<String, String> headerMap = new HashMap<>();
            System.out.println(p.toString());
            headerMap.put("Cookie","loginFrom=portal; X_PSS_GRAY=; PORTALSESSION=61fe6a8aca05007b6f59bcd880cb9f8274a05ed65922c295612a010b619433c24295f6453c8f1ff3f425aa7f163ad2f79c723cbce24e57970a81d6b1b14054afb3a5c73e9946c54a61504c8ebc96e8a9d94508e0f51ac07732b995c6d07a30fb3446731a75476bd8087f055faf5b10ef715c8e47fa9749e54169a9582ea1787d185e3be87d98c91220da0c5788cce6a493b75766c155bc2c426373f11218b67e; PASESSION=61fe6a8aca05007b6f59bcd880cb9f8274a05ed65922c295612a010b619433c24295f6453c8f1ff3f425aa7f163ad2f79c723cbce24e57970a81d6b1b14054afb3a5c73e9946c54a61504c8ebc96e8a9d94508e0f51ac07732b995c6d07a30fb3446731a75476bd8087f055faf5b10ef715c8e47fa9749e54169a9582ea1787d185e3be87d98c91220da0c5788cce6a493b75766c155bc2c426373f11218b67e; PATIMESTAMP=1692784660244_1692784699259; JSESSIONID=EFC1D7DCA562F22A95283400494C804C");
            String result = HttpUtil.createPost(url).headerMap(headerMap, true).body(p.toString()).execute().body();
            JSONObject user = JSONUtil.parseObj(result).getJSONObject("data").getJSONObject("clientBasicInfo");

            List<String> row = new ArrayList<>();
            row.add(user.getStr("clientName"));
            row.add(user.getStr("clientMobile"));
            row.add(user.getStr("idNo"));
            row.add(user.getStr("clientEmail"));
            row.add(user.getStr("contaAddress"));
            rows.add(row);
        }
        // Create Excel writer
        ExcelWriter writer = ExcelUtil.getWriter(outputFilePath);

        // Set the header row
        writer.writeHeadRow(columnList);

        // Write data rows
        writer.write(rows);

        // Close the writer and save the Excel file
        writer.close();

        System.out.println("done");
    }

}
